use ../dta/mf_hold, clear 
merge m:1 code date2 using ../dta/ranking_rolling
drop if _m==2
drop _m 

gen top150 = 0 
replace top150 = 1 if rank_t<=150 & rank_t!=.
replace q_rank_t_5=0 if q_rank_t_5==.


********* Row 1: Using Top Quintile Rank ****************

preserve 

local t  "q_rank_t_5" 
gen t = `t'
keep if date2>=ym(2011,12) & date2<=ym(2014,12)

local begin = ym(2011,12)
local end=ym(2014,12)

rename date2 reporting_date2
tempfile hold_temp
save `hold_temp'

forvalues x=`begin'(6)`end' {
	
	local y1 = `x' + 1
	local y2 = `x' + 6
	forvalues i= `y1'/`y2' {
		
		use `hold_temp', clear
		keep if reporting_date2 == `x'
		
		gen date2 = `i'
		merge m:1 code date2 using ../src/monthly_dgtw.dta
		drop if _m==2 
		drop _m 

		bysort fundid t date2: egen total_weight = sum(proportion)
		bysort fundid date2: egen tna = sum(marketvalue)
		collapse (mean) ret_dgtw ret ret_mkt [w=proportion], by(fundid t date2 total_weight tna) 
		tempfile temp_`i'
		save `temp_`i''
	}
}

clear
local begin = 1+`begin'
local end   = 6+`end'
forvalues i = `begin'/`end' {
	append using `temp_`i''
}

reshape wide ret_dgtw ret ret_mkt total_weight, i(fundid date2 tna) j(t)
gen hml = ret1 - ret0
gen hml_mkt = ret_mkt1 - ret_mkt0
gen hml_dgtw = ret_dgtw1 - ret_dgtw0


eststo clear
keep if date2>=ym(2012,1) & date2<=ym(2014,9) 

collapse hml* ret_dgtw1 ret_dgtw0 ret_mkt1 ret_mkt0 ret1 ret0 total_weight1  [w=1], by(date2)
tset date2
format date2 %tm

estpost sum total_weight1
eststo clear

eststo: newey ret1, lag(11) 
eststo: newey ret0, lag(11)
eststo: newey hml, lag(11)

eststo: newey ret_mkt1, lag(11)
eststo: newey ret_mkt0, lag(11)
eststo: newey hml_mkt, lag(11)


merge 1:1 date2 using "../src/ch4_m_2020.dta"
keep if _m==3
drop _m 

gen eret1 = ret1 - rf*100 
gen eret0 = ret0 - rf*100 
replace mktrf = mktrf*100

tset date2
eststo: newey eret1 mktrf, lag(11) 
eststo: newey eret0 mktrf, lag(11)
eststo: newey hml  mktrf, lag(11)

eststo: newey ret_dgtw1, lag(11)
eststo: newey ret_dgtw0, lag(11)
eststo: newey hml_dgtw, lag(11)

esttab using ../esttab/tab5_a.tex, keep(_cons) b(3) noobs star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes replace  frag coeflabel(_cons "Top Quintile") collabels(none)mlabels(none) nonumbers
eststo clear

restore 


******** Row 2: Using Top150 ********** 

local t  "top150" 

gen t = `t'
keep if date2>=ym(2011,12) & date2<=ym(2014,12)

local begin = ym(2011,12)
local end=ym(2014,12)

rename date2 reporting_date2
tempfile hold_temp2
save `hold_temp2'

forvalues x=`begin'(6)`end' {
	
	local y1 = `x' + 1
	local y2 = `x' + 6
	forvalues i= `y1'/`y2' {
		
		use `hold_temp2', clear
		keep if reporting_date2 == `x'
		
		gen date2 = `i'
		merge m:1 code date2 using ../src/monthly_dgtw
		drop if _m==2 
		drop _m 

		bysort fundid t date2: egen total_weight = sum(proportion)
		bysort fundid date2: egen tna = sum(marketvalue)
		collapse (mean) ret_dgtw ret ret_mkt [w=proportion], by(fundid t date2 total_weight tna)
		tempfile temp2_`i'
		save `temp2_`i'', replace
	}
}

clear
local begin = 1+`begin'
local end   = 6+`end'
forvalues i = `begin'/`end' {
	append using `temp2_`i''
}

reshape wide ret_dgtw ret ret_mkt total_weight, i(fundid date2 tna) j(t)
gen hml = ret1 - ret0
gen hml_mkt = ret_mkt1 - ret_mkt0
gen hml_dgtw = ret_dgtw1 - ret_dgtw0


eststo clear

keep if date2>=ym(2012,1) & date2<=ym(2014,9) /* v1-3*/ 

collapse hml* ret_dgtw1 ret_dgtw0 ret_mkt1 ret_mkt0 ret1 ret0 total_weight1  [w=1], by(date2)
tset date2
format date2 %tm

estpost sum total_weight1
eststo clear


eststo: newey ret1, lag(11) 
eststo: newey ret0, lag(11)
eststo: newey hml, lag(11)

eststo: newey ret_mkt1, lag(11)
eststo: newey ret_mkt0, lag(11)
eststo: newey hml_mkt, lag(11)


merge 1:1 date2 using "../src/ch4_m_2020.dta"
keep if _m==3
drop _m 

gen eret1 = ret1 - rf*100 
gen eret0 = ret0 - rf*100 
replace mktrf = mktrf*100

tset date2
eststo: newey eret1 mktrf, lag(11) 
eststo: newey eret0 mktrf, lag(11)
eststo: newey hml  mktrf, lag(11)

eststo: newey ret_dgtw1, lag(11)
eststo: newey ret_dgtw0, lag(11)
eststo: newey hml_dgtw, lag(11)

esttab using ../esttab/tab5_b.tex, keep(_cons) b(3) noobs star(* 0.10 ** 0.05 *** 0.01) nogaps nonotes replace  frag coeflabel(_cons "Top 150") collabels(none)mlabels(none) nonumbers


eststo clear
